package cn.dida.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.dida.model.LgsLogistics;
import cn.dida.model.LgsRelate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dida.dao.LgsDao;
import cn.dida.model.Pageable;
import cn.dida.service.LgsService;

@Service
public class LgsServiceImpl implements LgsService {
    @Autowired
    private LgsDao lgsDao;

	/**
	 * 批次查询
	 * @param companyId
	 * @param batchNnb
	 * @param page
	 * @return
	 */
	@Override
	public Pageable<Map<String,Object>> queryLgsInfoByBatch(Integer companyId, String batchNnb, Integer page) {

		return lgsDao.queryLgsInfoByBatch(companyId,batchNnb,page);
	}
	public List<Map<String,Object>> batchExport(Integer companyId, String batchNub){
		return lgsDao.batchExport(companyId,batchNub);
	}


	/**
	 * 物流查询   如果瓶码（bottleNo）不为空， 根据瓶码获取箱码； 根据箱码获取产品、瓶码、批次号等
	 * @param companyId
	 * @param bottleNo
	 * @param boxNo
	 * @return
	 */
	@Override
	public Map<String, Object> getLogistics(Integer companyId, String bottleNo, String boxNo){

		Map<String, Object> map = new HashMap<String, Object>();
		if(boxNo == null || boxNo.equals("")){
			boxNo = lgsDao.getBoxByBottle(bottleNo,companyId);
		}
		List<LgsLogistics> list = lgsDao.getLogistics(companyId, boxNo);
		List<LgsRelate> bottleNoList = lgsDao.getBottleByBox(boxNo,companyId);
		Map<String,Object> custScan=lgsDao.getCustScan(companyId, boxNo);
		if(custScan!=null && !custScan.isEmpty()){
			Long custScanTime = (Long) custScan.get("scanTime");
			if(list != null && list.size()>0){
				Long littleTime  = list.get(list.size()-1).getScanTime();
				if(littleTime>=custScanTime){
					map.put("lastRow",1);
				}else{
					for(LgsLogistics lgs:list){
						if(lgs.getScanTime()<custScanTime){
							map.put("scanTime",lgs.getScanTime());
							break;
						}
					}
				}
			}
			map.put("custScan",custScan);
		}

		map.put("list",list);
		map.put("bottleNoList",bottleNoList);
		String proName = ""; String batchNub = "";
		if(list!=null && list.size()>0){
			proName = list.get(0).getProName();
			batchNub =  list.get(0).getBatchNub();
		}


		map.put("proName", proName);
		map.put("batchNo", batchNub);
		map.put("boxNo", boxNo);
		return map;
	}


	/**
	 * 根据箱码获取瓶码
	 */
	public List<LgsRelate> getBottleByBox(String bQrcodeId,Integer companyId){
		return lgsDao.getBottleByBox(bQrcodeId,companyId);
	}

	/**
	 * 根据托盘码获取箱码
	 * @param trayQrcode
	 * @param companyId
	 * @return
	 */
	public List<LgsRelate> getBoxByTray(String trayQrcode,Integer companyId){

		List<Integer> qrCompanyIdList = lgsDao.getTrayCompany(trayQrcode,companyId);
		List<LgsRelate> list = new ArrayList<>();
		for(Integer qrCom:qrCompanyIdList){
			List<LgsRelate> li = lgsDao.getBoxByTray(trayQrcode,qrCom,companyId);
			list.addAll(li);
		}
//		return lgsDao.getBoxByTray(trayQrcode,companyId);
		return list;
	}

	/**
	 * 关联管理
	 * @param cTimeS 操作开始时间
	 * @param cTimeE 操作结束时间
	 * @param depot 操作地点id
	 * @param cAdmin 操作人id
	 * @param page
	 * @return
	 */
	@Override
	public Pageable<LgsRelate> getRelateBox(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin,Integer page){

		return lgsDao.getRelateBox(companyId,cTimeS,cTimeE,depot,cAdmin,page);
	}

	/**
	 * 关联管理  托盘关联
	 * @param companyId
	 * @param cTimeS
	 * @param cTimeE
	 * @param depot
	 * @param cAdmin
	 * @param page
	 * @return
	 */
	public Pageable<LgsRelate> getRelateTray(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin,Integer page){
		return lgsDao.getRelateTray(companyId,cTimeS,cTimeE,depot,cAdmin,page);
	}

	@Override
	public List<LgsRelate> relateBoxExport(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin){
		return lgsDao.relateBoxExport(companyId, cTimeS, cTimeE, depot, cAdmin);
	}

	public List<LgsRelate> relateTrayExport(Integer companyId,String cTimeS, String cTimeE, Integer depot, String cAdmin){
		return lgsDao.relateTrayExport(companyId, cTimeS, cTimeE, depot, cAdmin);
	}
	
    
}
